package com.xinqing.test.LetCode;

/**
 * @Author:CHQ
 * @Date:2021/7/20 14:02
 * @Description
 */
public class GetKthFromEnd {

    public static void main(String args[]) {
        ListNode a = new ListNode(1);
        ListNode b = new ListNode(2);
        ListNode c = new ListNode(3);
        a.next = b;
        b.next = c;

        System.out.println(test(a, 2).toString());
    }

    public static ListNode test(ListNode head, int x) {
        int count = 0;
        ListNode temp = head;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        System.out.println(count);

        for (int i = 1; i <= count - x; i++) {
            head = head.next;
        }

        return head;
    }

}

class ListNode {
    ListNode next;
    int val;

    public ListNode(int val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return "ListNode={" +
                "val=" + val + "," +
                "next=" + next +
                "}";
    }
}



